package com.fram.controller.admin;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fram.bean.admin.user.request.LoginRequest;
import com.fram.bean.admin.user.request.RegisterAdminUserRequest;
import com.fram.bean.admin.user.request.UpdateAdminUserPasswordRequest;
import com.fram.bean.sys.ResponseMsg;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProAffairException;
import com.fram.common.exception.ProException;
import com.fram.common.util.PublicMethod;
import com.fram.common.util.apiversion.ApiVersion;
import com.fram.common.util.base.BaseAdminController;
import com.fram.service.admin.AdminUserService;

/**后台-用户管理 查看 --前台 登录 等**/
@RestController
@RequestMapping("/admin/{version}/user")
public class AdminUserController extends BaseAdminController {

	private static Logger LOG = LoggerFactory.getLogger(AdminUserController.class);

	@Resource(name = "adminUserService")
	private AdminUserService adminUserService;

	/** 后台登录 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "/login")
	private ResponseMsg login(@RequestBody LoginRequest request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(adminUserService.login(request));
		} catch (ProException e) {
			LOG.error("-后台用户-后台登录出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-后台用户-后台登录异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 查询用户的菜单 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "auth/userMenu")
	private ResponseMsg userMenu(Long parentId) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(adminUserService.userMenu(getAdminParamUserId(), parentId));
		} catch (ProException e) {
			LOG.error("-后台用户-查询用户的菜单出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-后台用户-查询用户的菜单异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 后台管理员注册 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "/register")
	private ResponseMsg register(@RequestBody RegisterAdminUserRequest request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			adminUserService.registerAdminUser(request);
		} catch (ProAffairException e) {
			LOG.error("-后台用户-注册新的管理员出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-后台用户-注册新的管理员异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 退出登录 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "auth/logout")
	private ResponseMsg logout() {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			adminUserService.logout(getRedisKey());
		} catch (ProAffairException e) {
			LOG.error("-后台用户-退出登录 出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-后台用户-退出登录 异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 修改密码 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "auth/updatepassword")
	private ResponseMsg updatepassword(@RequestBody UpdateAdminUserPasswordRequest request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			request.setUserId(getAdminUserId());
			adminUserService.updatePassword(request, getRedisKey());
			msg.setReturnMsg("修改密码成功,请重新登录");
		} catch (ProException e) {
			LOG.error("-后台用户-修改密码->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-后台用户-修改密码->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
}
